import Mock from 'mockjs';
import Http from '@/lib/http';
import './fetch';

function makeResult (result) {
  return {
    success: true,
    code: '0000',
    desc: '',
    result: result,
  };
}

const modulesFiles = require.context('./modules', true, /\.js$/);
const modules = modulesFiles.keys().reduce((modules, modulePath) => {
  // set './app.js' => 'app'

  const moduleName = modulePath.replace(/^\.\/(.*)\.\w+$/, '$1');

  const value = modulesFiles(modulePath);

  modules[moduleName] = value.default;

  if (value.default) {
    value.default.forEach(item => {
      Mock.mock(
        new RegExp(Http.getUrl(item.url)),
        item.isAllResult ? item.result : makeResult(item.result),
      );
    });
  }
  return modules;
}, {});

console.log('modules', modules);
